package com.mjk.manager.magicapi.starter;

import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.magicapi.core.core.config.MagicAPIProperties;
import com.mjk.manager.magicapi.core.utils.PathUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;

/**
 * 输出服务访问地址
 *
 * @author 冰点
 * @date 2021-6-3 12:08:59
 * @since 1.2.1
 */
@Configuration
@ConditionalOnProperty(name = "magic-api.show-url", havingValue = "true",matchIfMissing = true)
@Order
public class ApplicationUriPrinter implements CommandLineRunner {

    @Value("${server.port:8080}")
    private int port;

    @Value("${server.servlet.context-path:}")
    private String contextPath;

    @Value("${spring.boot.admin.client.url:}")
    private String adminUrl;

    @Autowired
    private MagicAPIProperties properties;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("********************************************当前服务相关地址********************************************");
        String ip = "IP";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            System.out.println("当前服务地址获取失败");
        }
        String magicWebPath = properties.getWeb();
        String schema = "http://";
        String localUrl = schema + PathUtils.replaceSlash(String.format("localhost:%s/%s/%s/", port, contextPath, Objects.toString(properties.getPrefix(), "")));
        String externUrl = schema + PathUtils.replaceSlash(String.format("%s:%s/%s/%s/", ip, port, contextPath, Objects.toString(properties.getPrefix(), "")));
        System.out.printf(
                "服务启动成功! Access URLs:" +
                        "\n\t接口本地地址: \t\t%s" +
                        "\n\t接口外部地址: \t\t%s\n"
                , localUrl
                , externUrl
        );
        if (!StringUtils.isEmpty(magicWebPath)) {
            String webPath = schema + PathUtils.replaceSlash(String.format("%s:%s/%s/%s/index.html", ip, port, contextPath, magicWebPath));
            System.out.println("\tMagicApi地址: \t\t" + webPath);
        }
        //打印plumelog地址
        System.out.println("\tplumelog地址: \t\t" + (schema + String.format("%s:%s/%s", ip, port, "plumelog")));
        //打印积木报表地址
        System.out.println("\t积木报表地址: \t\t" + (schema + String.format("%s:%s/%s", ip, port, "jmreport/list")));
        //swagger地址
        System.out.println("\tswagger地址: \t\t" + (schema + String.format("%s:%s/%s", ip, port, "swagger-ui/index.html")));
        if (ObjectUtils.isNotEmpty(adminUrl)) {
            System.out.println("\tsba监控地址: \t\t\t" + adminUrl + "\t(SpringBoot Admin)");
        }
        //mamis 地址
        System.out.println("\t前端渲染地址: \t\t" + (schema + String.format("%s:%s/%s", ip, port, "mamis")));
        //promethous指标地址
        System.out.println("\t指标地址: \t\t\t" + (schema + String.format("%s:%s/%s", ip, port, "actuator")));
        System.out.println("\t可通过配置关闭输出: \tmagic-api.show-url=false");
        System.out.println("********************************************当前服务相关地址********************************************");
    }
}
